package org.kjtc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.kjtc.entity.ActionHistory;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LogController extends BaseController {

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value="/searchLog")
	@ResponseBody
	public Map<String, Object> searchLog( String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "日志查询开始");
		List<String> list = new ArrayList<String>();
		list.add("kjtc2016.log");
		map.put("log", list);
		/*		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<Logger>loggers=null;
			try {
			loggers = logservice.find(logger, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    Map<String, Object> map = new HashMap<String, Object>();
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("logList",loggers );*/
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "日志查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "日志查询");
		return map;
	}

	@RequestMapping(value="/downloadLog")
	@ResponseBody
	public void downloadLog(HttpServletResponse response){

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "日志下载开始");
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "日志下载结束");
	}

	@RequestMapping (value="/searchUserLog")
	@ResponseBody
	public Map<String, Object> searchUserLog(ActionHistory actionHistory, String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户操作日志查询开始");
		// 分页查询
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		actionHistory.setRoleID(this.getUesrInfo().getRoleID());

		List<ActionHistory> list = null;
		try {
			list = actionHistoryService.getActionHistoryPage(actionHistory, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户操作日志查询异常" + e.getMessage());
		}
		map.put("totalCnt", pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("userLog", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户操作日志查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户日志查询");
		return map;
	}

}
